/* file:dir.cc
 * system independence directory operations.
 */

#include "dir.h"
#ifdef __DEBUG__
#include <iostream>
using namespace std;
#endif

#if defined (WIN32)
#include "dirent.h"
#include "io.h"
/**
 * Filter is NULL means no filter
 */
vector<string> DIROprations::get_all_filename(const char *dirname, const char *filter)
{
	DIR *pdir = NULL;
	struct dirent* ep = NULL;
	vector<string> fnames;
	char *tempptr = NULL;

	pdir = opendir(dirname);
	if (NULL != pdir) {
		ep = readdir(pdir);
		while (NULL != ep) {
            if ((ep->attrib & _A_SUBDIR) == 0) {
			    tempptr = strrchr(ep->d_name, '.');
                if ( NULL == filter) {
				    fnames.push_back(ep->d_name);
                } else if ( (NULL != tempptr) && (0 == strcmp(tempptr, filter)) ) {
				    fnames.push_back(ep->d_name);
			    }
            }
			ep = readdir(pdir);
		}
		closedir(pdir);
	}

	return fnames;
}
//#elif defined (LINUX)
#else
#include <sys/types.h>
#include <dirent.h>
/**
 * Filter is NULL means no filter
 */
vector<string> DIROprations::get_all_filename(const char *dirname, const char *filter)
{
	DIR *pdir = NULL;
	struct dirent* ep = NULL;
	vector<string> fnames;
	char *tempptr = NULL;

	pdir = opendir(dirname);
	if (NULL != pdir) {
		ep = readdir(pdir);
		while (NULL != ep) {
            //is a regular file?
            if(ep->d_type == DT_REG) {
                tempptr = strrchr(ep->d_name, '.');
                if ( NULL == filter) {
                    fnames.push_back(ep->d_name);
                } else if ( (NULL != tempptr) && (0 == strcmp(tempptr, filter)) ) {
                    fnames.push_back(ep->d_name);
                }
            }
			ep = readdir(pdir);
		}
		closedir(pdir);
	}

	return fnames;
}
#endif //__WIN32__



string DIROprations::build_fullpath(string dir, string filename)
{
    string retstring = dir;
    retstring.append("/");
    retstring.append(filename);
    return retstring;
}


